package com.molyfun.parents.service;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.molyfun.parents.activity.common.VideoPlayerActivity;
import com.molyfun.parents.service.music.MusicPlayService;
import com.molyfun.parents.service.music.MusicStatusCenter;
import com.molyfun.parents.utils.Const;

public class MediaUtils {

    private static final String TAG = "MediaUtils";

    public static final String ACTION_MEDIA_PLAY = MusicPlayService.ACTION_PLAY;
    public static final String ACTION_MEDIA_STOP = MusicPlayService.ACTION_STOP;
    public static final String ACTION_MEDIA_PAUSE = MusicPlayService.ACTION_PAUSE;
    public static final String ACTION_MEDIA_RESUME = MusicPlayService.ACTION_RESUME;

    public static boolean startPlayAudio(Context context, String media) {
        if (!TextUtils.isEmpty(media)) {
            Intent intent = new Intent();
            intent.setClass(context.getApplicationContext(), MusicPlayService.class);
            intent.setAction(MusicPlayService.ACTION_PLAY);
            intent.putExtra(MusicPlayService.FLAG_MUSIC_URL, media);
            context.startService(intent);
            return true;
        }
        return false;
    }

    public static boolean stopPlayAudio(Context context) {
        Intent intent = new Intent();
        intent.setClass(context.getApplicationContext(), MusicPlayService.class);
        intent.setAction(MusicPlayService.ACTION_STOP);
        context.startService(intent);
        return true;
    }

    public static boolean pausePlayAudio(Context context) {
        Intent intent = new Intent();
        intent.setClass(context.getApplicationContext(), MusicPlayService.class);
        intent.setAction(MusicPlayService.ACTION_PAUSE);
        context.startService(intent);
        return true;
    }

    public static boolean resumePlayAudio(Context context) {
        Intent intent = new Intent();
        intent.setClass(context.getApplicationContext(), MusicPlayService.class);
        intent.setAction(MusicPlayService.ACTION_RESUME);
        context.startService(intent);
        return true;
    }

    public static boolean isPlaying(String media) {
        return MusicStatusCenter.isCurrentPlayingMusicItem(media);
    }

    public static boolean isPaused(String media) {
        if (!isPlaying(media)) {
            return false;
        }
        return isCurrentPaused();
    }

    public static boolean isCurrentPaused() {
        MusicPlayService.State state = getCurrentPlayingAudioState();
        if (state != null && state == MusicPlayService.State.Paused) {
            return true;
        }
        return false;
    }

    public static MusicPlayService.State getCurrentPlayingAudioState() {
        return MusicStatusCenter.getCurrentPlayingAudioState();
    }

    public static String getCurrentPlayingAudio() {
        return MusicStatusCenter.getCurrentPlayingAudio();
    }

    public static boolean isCurrentPlayingAudio() {
        return MusicStatusCenter.isCurrentPlayingAudio();
    }

    /**
     * 视频播放界面
     */
    public static void startVideo(Context context, String url, String title) {
        Intent intent = new Intent();
        intent.setClass(context.getApplicationContext(), VideoPlayerActivity.class);
        intent.putExtra(Const.FLAG_VIDEO_URL, url);
        intent.putExtra(Const.FLAG_VIDEO_TITLE, title);
        context.startActivity(intent);
    }

    public static void startVideoAndStopAudio(Context context, String url, String title) {
        stopPlayAudio(context);
        startVideo(context, url, title);
    }


//    public static void sendPlayAction(Context context) {
//        Intent intent = new Intent(ACTION_MEDIA_PLAY);
//        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
//    }
//
//    public static void sendStopAction(Context context) {
//        Intent intent = new Intent(ACTION_MEDIA_STOP);
//        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
//    }
//
//    public static void sendPauseAction(Context context) {
//        Intent intent = new Intent(ACTION_MEDIA_PAUSE);
//        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
//    }

}
